package com.ziyucoding.springboot.labs.lab4.concurrentdemo.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * FutureTask 示例1
 *
 * @author ziyucoding
 * @date 2022-10-27
 **/
@Slf4j
public class FutureTaskExample1 {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {

            @Override
            public String call() throws Exception {
                log.info("do something in callable");
                Thread.sleep(3000);
                return "Done";
            }
        });

        new Thread(futureTask).start();
        log.info("do something in main");
        Thread.sleep(1000);
        // 任务没结束的话，会被一直阻塞
        // FutureTask使用方便，建议使用
        String result = futureTask.get();
        log.info("result:{}", result);
    }

}
